
/**
 * 科目详情对话框
 */
var ZxKemuInfoDlg = {
    data: {
        btyZxKemuId: "",
        name: ""
    }
};

layui.use(['layer', 'table', 'admin',  'form','ax','upload'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;

    var ajax_cb = new $ax(Feng.ctxPath + "/dgdzxBx/detail?btyZxChengbenId=" + Feng.getUrlParam("btyZxChengbenId"));
    var result_cb = ajax_cb.start();
    $("#zongjia").val(result_cb.data.zongjia);
    $("#zhaiyao").val(result_cb.data.zhaiyao);
    /**
     * 管理
     */
    var ZxChengbenMingxi = {
        tableId: "zxChengbenMingxiTable"
    };

    form.verify({
        double: function (value) {
            var patt2=/^(\-|\+)?\d+(\.\d+)?$/;
            if (patt2.test(value) == false) {
                return '你输入的不是数字';
            }
        }
    });

    /**
     * 初始化表格的列
     */
    ZxChengbenMingxi.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'btyZxChengbenMingxiId', hide: true, title: ''},
            {field: 'btyBrandIdView', sort: true, title: '门店'},
            {field: 'btyZxChengbenId', sort: true, hide: true,title: '成本'},
            {field: 'btyZxKemuIdView', sort: true, title: '科目'},
            {field: 'jine', sort: true, title: '金额',totalRow: true},
            {field: 'gongshi', sort: true, title: '公式',totalRow: true},
            {field: 'qtkm', sort: true, title: '其他科目'},
            {field: 'beizhu', sort: true, title: '备注'},
            // {field: 'status', sort: true, title: '状态'},
            // {field: 'statusView', sort: true, title: '状态显示'},
            // {field: 'delFlag', sort: true, title: ''},
            // {field: 'createUser', sort: true, title: ''},
            // {field: 'createUserName', sort: true, title: ''},
            // {field: 'createTime', sort: true, title: ''},
            // {field: 'updateUser', sort: true, title: ''},
            // {field: 'updateUserName', sort: true, title: ''},
            // {field: 'updateTime', sort: true, title: ''},
            // {field: 'company', sort: true, title: ''},
            // {field: 'companyView', sort: true, title: ''},
            // {field: 'srcType', sort: true, title: ''},
            // {field: 'srcValue', sort: true, title: ''},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ZxChengbenMingxi.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(ZxChengbenMingxi.tableId, {where: queryData});
    };

    /**
     * 弹出添加对话框
     */
    ZxChengbenMingxi.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/dgdzxBxMingxi/add';
    };

    /**
     * 导出excel按钮
     */
    ZxChengbenMingxi.exportExcel = function () {
        var checkRows = table.checkStatus(ZxChengbenMingxi.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ZxChengbenMingxi.openEditDlg = function (data) {
        var url = Feng.ctxPath + '/dgdzxBxMingxi/edit?btyZxChengbenMingxiId=' + data.btyZxChengbenMingxiId+"&btyZxChengbenId="+data.btyZxChengbenId
            +"&btyZxKemuId="+data.btyZxKemuId+"&btyZxKemuIdView="+encodeURIComponent(data.btyZxKemuIdView);
        // window.location.href = url;

        parent.layer.open({
            type: 2,
            title: '修改门店科目',
            area: ['700px', '500px'],
            content: url,
            end: function () {
                ZxChengbenMingxi.search();
            },
            cancel:function (index){
                ZxChengbenMingxi.search();
            }
        });

    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ZxChengbenMingxi.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/dgdzxBxMingxi/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ZxChengbenMingxi.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("btyZxChengbenMingxiId", data.btyZxChengbenMingxiId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ZxChengbenMingxi.tableId,
        //参数来自zxChengben.js
        url: Feng.ctxPath + '/dgdzxBxMingxi/list?btyZxChengbenId=' + Feng.getUrlParam("btyZxChengbenId"),
        page: true,
        height: "full-128",
        cellMinWidth: 100,
        totalRow: true,
        cols: ZxChengbenMingxi.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ZxChengbenMingxi.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ZxChengbenMingxi.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ZxChengbenMingxi.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ZxChengbenMingxi.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ZxChengbenMingxi.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ZxChengbenMingxi.onDeleteItem(data);
        }
    });

    var loading;
    layui.upload.render({
        elem: '#btnImportMdbx'
        , url: '/dgdzxBxMingxi/btnImportMdbx'
        , accept: "file"
        , async: false
        , data: {"btyZxChengbenId":Feng.getUrlParam("btyZxChengbenId")}
        , exts: 'xls|xlsx'
        , before: function () {
            //this.data = {'dataTime': $('#dataTime').val()};//关键代码
            loading = layer.load(2, {shade: [0.15, '#ccc']});
        }
        , done: function (data) {
            if (data.code == 200) {
                layer.close(loading);
                Feng.success("导入成功");
            } else {
                layer.close(loading);
                Feng.error(data.message);
            }
            ZxChengbenMingxi.search();
        }
    });

    // 点击科目时
    $('#btyZxKemuIdView').click(function () {
        var formName = encodeURIComponent("parent.ZxKemuInfoDlg.data.name");
        var formId = encodeURIComponent("parent.ZxKemuInfoDlg.data.btyZxKemuId");
        var treeUrl = encodeURIComponent("/zxKemu/tree");

        layer.open({
            type: 2,
            title: '科目选择（请双击选择）',
            area: ['50%', '80%'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#btyZxKemuId").val(ZxKemuInfoDlg.data.btyZxKemuId);
                $("#btyZxKemuIdView").val(ZxKemuInfoDlg.data.name);
            }
        });
    });


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/dgdzxBxMingxi/addItem", function (data) {
            Feng.success(data.message);
            //重置输入框
            //刷新列表
            var queryData = {};
            queryData['btyZxChengbenId'] = Feng.getUrlParam("btyZxChengbenId");
            table.reload(ZxChengbenMingxi.tableId, {where: queryData});
            table.reload(ZxChengbenMingxi.tableId);
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });

        ajax.set("btyZxChengbenId",Feng.getUrlParam("btyZxChengbenId"));
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    $('#auditPass').click(function(){
        layer.confirm('确定通过?', {icon: 3, title:'提示'}, function(index){
            var ajax = new $ax(Feng.ctxPath + "/dgdzxBx/audit", function (data) {
                if(data.success){
                    Feng.success('操作成功');
                }
            }, function (data) {
                Feng.error("操作失败！" + data.responseJSON.message)
            });
            ajax.set("btyZxChengbenId",Feng.getUrlParam("btyZxChengbenId"));
            ajax.set("status","2");
            ajax.start();
            layer.close(index);
        });
    });
    $('#auditReject').click(function(){
        layer.confirm('确定驳回?', {icon: 2, title:'提示'}, function(index){
            var ajax = new $ax(Feng.ctxPath + "/dgdzxBx/audit", function (data) {
                if(data.success){
                    Feng.success('操作成功');
                }
            }, function (data) {
                Feng.error("操作失败！" + data.responseJSON.message)
            });
            ajax.set("btyZxChengbenId",Feng.getUrlParam("btyZxChengbenId"));
            ajax.set("status","5");
            ajax.start();
            layer.close(index);
        });
    });

    function closePage(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }

    //返回按钮
    $("#backupPage").click(function () {
        closePage();
        // window.location.href = Feng.ctxPath + '/zxChengben'
    });


    //获取成本详情信息，填充门店选择框
    var ajax = new $ax(Feng.ctxPath + "/dgdzxBx/detail?btyZxChengbenId=" + Feng.getUrlParam("btyZxChengbenId"));
    var result = ajax.start();

    /**
     * 初始门店
     */
    var languageAjax = new $ax(Feng.ctxPath + "/mdzx/mdzxList?btyZxChengbenId="+Feng.getUrlParam("btyZxChengbenId"));
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        if(val.btyBrandIdView!=undefined){
            var pinyin = ConvertPinyin(val.btyBrandIdView);
            //添加的门店
            $("#btyZxMdzxId").append("<option value='" + val.btyZxMdzxId + "'>" + val.btyBrandIdView +"-"+ pinyin + "</option>");
        }
    });
    if(result.data.btyZxMdzxId!=null&&result.data.btyZxMdzxId!=""){
        $('#btyZxMdzxId').val(result.data.btyZxMdzxId);
    }
    form.render('select');

    //金额与公式联动
    $("#jine").on("input",function(e){
       var jine=$("#jine").val();
        $("#gongshi").val("");
        if(jine!=""){
            $("#gongshi").val(jine);
        }
    });


});
